/**
 * 
 */
package com.douyu.ocean.recom.platfrom.core.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.douyu.ocean.recom.platform.api.model.OceanResultSource;
import com.douyu.ocean.recom.platform.api.model.RuleTemplate;
import com.douyu.ocean.recom.platform.api.model.entiry.RecomBiz;
import com.douyu.ocean.recom.platform.api.model.entiry.RecomPos;
import com.douyu.ocean.recom.platform.api.model.entiry.RecomRule;
import com.douyu.ocean.recom.platform.api.utils.RedisBizSourceUtil;
import com.douyu.ocean.recom.platfrom.core.exception.BusinessException;
import com.douyu.ocean.recom.platfrom.core.exception.CommonErrorCode;
import com.douyu.ocean.recom.platfrom.core.service.RecomBizService;
import com.douyu.ocean.recom.platfrom.core.service.RecomPosService;
import com.douyu.ocean.recom.platfrom.core.service.RecomRuleService;
import com.douyu.ocean.recom.platfrom.core.service.RuleTemplateService;

/**
 * @author leiruiqi
 *
 */
@Service("ruleTemplateService")
public class RuleTemplateServiceImpl implements RuleTemplateService {

	@Resource
	private RecomBizService recomBizService;
	
	@Resource
	private RecomPosService recomPosService;
	
	@Resource
	private RecomRuleService recomRuleService;
	
	@Override
	public RuleTemplate getRuleTemplateByBiz(String bizCode) {
		RecomBiz recomBiz = recomBizService.getByCode(bizCode);
		checkBizEffect(recomBiz);
		
		RecomRule recomRule = recomRuleService.getById(recomBiz.getRuleId());
		checkRuleEffect(recomRule);
		
		RecomPos recomPos = recomPosService.getById(recomBiz.getPosId());
		RuleTemplate template = new RuleTemplate();
		
		buildTemplateByRule(template,recomRule,recomBiz,recomPos);
		
		return template;
	}

	
	@Override
	public RuleTemplate getRuleTemplateByRule(String ruleCode) {
		
		RecomRule rule = recomRuleService.getByCode(ruleCode);
		checkRuleEffect(rule);
		RuleTemplate template = new RuleTemplate();
		buildTemplateByRule(template,rule);
		return template;
	}
	
	private void buildTemplateByRule(RuleTemplate template,RecomRule rule){
		template.setRuleCode(rule.getCode());
		Map<String,String> ruleParamMap = JSON.parseObject(rule.getParamsStr(),
				new TypeReference<Map<String, String>>() {
				});
		template.setRuleParamMap(ruleParamMap);
	}
	
	private void buildTemplateByRule(RuleTemplate template,RecomRule rule,RecomBiz recomBiz,RecomPos recomPos){
		template.setRuleCode(rule.getCode());
		Map<String,String> ruleParamMap = JSON.parseObject(rule.getParamsStr(),
				new TypeReference<Map<String, String>>() {
				});
		template.setRuleParamMap(ruleParamMap);
		
		template.setPosCode(recomPos.getCode());
		
		template.setBypassExpression(recomBiz.getBypassExpression());
		
		List<OceanResultSource> bizSourceList = RedisBizSourceUtil.getBizSourceListBySourceStr(recomBiz.getSourceStr());
		template.setSourceList(bizSourceList);
		
		Map<String,String> bizParamMap = JSON.parseObject(recomBiz.getParamsStr(),
				new TypeReference<Map<String, String>>() {
				});
		template.setBizParamMap(bizParamMap);
		
	}
	
	private void checkBizEffect(RecomBiz recomBiz){
		if(recomBiz == null ||recomBiz.getStatus()!=1){
			throw new BusinessException(CommonErrorCode.BUSINESS_ERROR,"规则无效");
		}
	}
	
	private void checkRuleEffect(RecomRule rule){
		if(rule == null ||rule.getStatus()!=1){
			throw new BusinessException(CommonErrorCode.BUSINESS_ERROR,"规则无效");
		}
		
		
	}

}
